package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.business.TsOrganizationAccount;
import com.ys.enume.sys.RegexUtilEnum;
import com.ys.mapper.business.TsOrganizationAccountMapper;
import com.ys.service.business.TsOrganizationAccountService;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("TsOrganizationAccountService")
public class TsOrganizationAccountImpl implements TsOrganizationAccountService {

    @Autowired
    private TsOrganizationAccountMapper tsOrganizationAccountMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {

        TsOrganizationAccount tsOrganizationAccount = JSON.parseObject(JSON.toJSONString(params), TsOrganizationAccount.class);
        if (new TsOrganizationAccount().setOrgid(tsOrganizationAccount.getOrgid()).count() != 0){
            return R.error("该部门已绑定，核实后再操作!");
        }
        tsOrganizationAccountMapper.insert(tsOrganizationAccount);
        return R.ok(AddDto.closeAndReload());
    }

    /**
     * 修改
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {
        TsOrganizationAccount tsOrganizationAccount = JSON.parseObject(JSON.toJSONString(params), TsOrganizationAccount.class);
        if (new TsOrganizationAccount().setId(tsOrganizationAccount.getId()).where("A.id != " + tsOrganizationAccount.getId()).count() != 0){
            return R.error("该工号/姓名的数据已存在，核实后再操作!");
        }
        if (new TsOrganizationAccount().setOrgid(tsOrganizationAccount.getOrgid()).where("A.id != " + tsOrganizationAccount.getId()).count() != 0){
            return R.error("该部门已绑定，核实后再操作!");
        }
        tsOrganizationAccountMapper.update(tsOrganizationAccount);
        return R.ok(AddDto.gotoEdit("/web/business/tsOrganizationAccount/toEdit?id=" + tsOrganizationAccount.getId()));
    }

}
